package com.kunya.product.service.impl.active;

import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.product.ProductMapper;
import com.kunya.product.dao.QuestionMapper;
import com.kunya.product.po.Product;
import com.kunya.product.po.Question;
import com.kunya.product.service.active.IQuestionService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author WangGang
 * @Description: 商品提问接口实现类
 * @date 2018/7/11 10:15
 */
@Service("iQuestionService")
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    /**
     * @Author WangGang
     * @Description 商品提问提交
     * @Date 10:18 2018/7/11
     * @Param [question, productId, tablePrefix]
     **/
    public ServerResponse<Question> insertQuestion(Question question, Long productId, String tablePrefix) {

        Product product = productMapper.selectByPrimaryKey(productId, tablePrefix);
        if(product!=null){
            question.setProductId(productId);
            question.setCreateTime(new Date());
            question.setCreateBy(question.getUserName());
            questionMapper.insertQuestion(question,tablePrefix);
        }
        return ServerResponse.createBySuccessMessage("商品提问提交成功");
    }

    @Override
    /**
     * @Author WangGang
     * @Description 通过商品ID查询商品提问列表
     * @Date 10:20 2018/7/11
     * @Param [productId,tablePrefix]
     **/
    public ServerResponse<List<Question>> listByProductId(Long productId,String tablePrefix) {
        if (productId == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        List<Question> questionList = questionMapper.selectQuestionsByProductId(productId, tablePrefix);
        if(CollectionUtils.isEmpty(questionList)){
            return ServerResponse.createByErrorMessage("商品提问不存在");
        }
        return ServerResponse.createBySuccess(questionList);
    }

    @Override
    /**
     * @Author WangGang
     * @Description 根据提问ID和商品ID查询商品提问详情
     * @Date 9:28 2018/7/12
     * @Param [questionId, productId, tablePrefix]
     **/
    public ServerResponse<Question> detailsByQuestionId(Long questionId, Long productId,String tablePrefix) {
        if(questionId==null || productId==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        Question question=questionMapper.detailsByQuestionId(questionId,productId,tablePrefix);
        if(question==null){
            return ServerResponse.createBySuccessMessage("商品提问详情不存在");
        }
        return ServerResponse.createBySuccess(question);
    }

    @Override
    /**
     * @Author WangGang
     * @Description 根据提问ID添加商家回复
     * @Date 14:57 2018/7/12
     * @Param [questionId, question, tablePrefix]
     **/
    public ServerResponse<Question> reply(Long questionId, Question question, String tablePrefix) {
        if(questionId==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        question.setAnswerContent(question.getAnswerContent());
        question.setAnswerBy(question.getAnswerBy());
        question.setAnswerTime(new Date());
        questionMapper.reply(question,questionId,tablePrefix);
        return ServerResponse.createBySuccess();
    }
}
